package me.hys.service.doctor.impl;

import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

import me.hys.base.ApiResult;
import me.hys.base.PageQuery;
import me.hys.bo.doctor.CommentBo;
import me.hys.dao.doctor.ICommentMapper;
import me.hys.dao.system.IUserMapper;
import me.hys.entity.doctor.CommentEntity;
import me.hys.entity.system.UserEntity;
import me.hys.service.doctor.ICommentManager;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Author: @author HYS . <br>
 * Date: 2015-10-7 下午9:40:50 . <br>
 * Function:
 */
@Service
@Transactional
class CommentManagerImpl implements ICommentManager {
	@Autowired
	private ICommentMapper commentMapper;
	@Autowired
	private IUserMapper userMapper;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * me.hys.service.doctor.ICommentManager#findPage(me.hys.base.PageQuery)
	 */
	@Override
	public PageQuery<CommentBo> findPage(PageQuery<CommentBo> page) {
		UserEntity userEntity = userMapper.findById(String.valueOf(page
				.getCondition().get("userId")));
		if (null == userEntity) {
			return page;
		}
		Map<String, Object> condition = page.getCondition();
		condition.put("type", userEntity.getType());
		List<CommentBo> list = commentMapper.findPage(condition);
		page.setList(list);
		page.setTotal(commentMapper.findPageCount(condition));
		// 总页数
		int size = (int) Math.ceil(page.getTotal() * 1.0f / page.getLimit());
		page.setSize(size);
		return page;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * me.hys.service.doctor.ICommentManager#save(me.hys.entity.doctor.CommentEntity
	 * )
	 */
	@Override
	public ApiResult save(CommentEntity entity) {
		ApiResult apiResult = new ApiResult();
		entity.setCreateTime(new Timestamp(System.currentTimeMillis()));
		commentMapper.save(entity);
		apiResult.setResult(entity.getId());
		apiResult.setMsg("评论成功");
		return apiResult;
	}
}
